<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table ref="multipleTable" size='mini' :data="ipsList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="id" label="ID" align='center' min-width="70">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" align='center' :label="$t('策略名称')" min-width="120" show-overflow-tooltip></el-table-column>
      <el-table-column prop="class_map" align='center' :label="$t('规则集')" min-width="500">
        <template slot-scope="scope">
          <tooltip :html="classMapShowTip(scope.row)" :content="classMapShow(scope.row)" class="wid190" :refName="scope.row.name"></tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="ref" align='center' :label="$t('引用计数')" min-width="100"></el-table-column>
      <el-table-column align='center' width='100' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <ips-dialog v-if="ipsDialogVisible" @listData='listData' ref="refIpsDialogVisible"></ips-dialog>
  </div>
</template>
<script>
import { getIpsArray, delIps } from '@/api/safe/ips'
import IpsDialog from './components/IpsDialog.vue'
import ipsList from './ipsArray'
export default {
  name: 'Ips',
  components: {
    IpsDialog
  },
  data () {
    return {
      ipsDialogVisible: false,
      ipsList: [],
      selectList: [],
      nameStr: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      this.selectList = data
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getIpsArray().then(res => {
        if (res.status === 0) {
          this.ipsList = res.info
        }
      })
    },
    // 添加按钮
    dialogVisible (data) {
      this.ipsDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refIpsDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delIps({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = []
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    classMapShowTip (data) {
      let reStr = ''
      let dataList = []
      for (const index in data.class_map) {
        if (index === '0') {
          dataList.push(data.class_map[index])
          if (data.class_map.length <= 1) {
            reStr += this.$t(ipsList[dataList[0].class_type]) + ':' + this.$t(ipsList[dataList[0].class_name]) + ';<br/>'
          }
        } else {
          if (data.class_map[index].class_type === data.class_map[index - 1].class_type) {
            dataList.push(data.class_map[index])
          } else {
            for (const childIndex in dataList) {
              if (childIndex === '0') {
                reStr += this.$t(ipsList[dataList[childIndex].class_type]) + ':' + this.$t(ipsList[dataList[childIndex].class_name])
              } else {
                reStr += ',' + this.$t(ipsList[dataList[childIndex].class_name])
              }
              if (Number(childIndex) === dataList.length - 1) {
                reStr += ';<br/>'
                dataList = []
              }
            }
            dataList.push(data.class_map[index])
          }
          if (data.class_map.length - 1 === Number(index)) {
            for (const childIndex2 in dataList) {
              if (childIndex2 === '0') {
                reStr += this.$t(ipsList[dataList[childIndex2].class_type]) + ':' + this.$t(ipsList[dataList[childIndex2].class_name])
              } else {
                reStr += ',' + this.$t(ipsList[dataList[childIndex2].class_name])
              }
              if (Number(childIndex2) === dataList.length - 1) {
                reStr += ';<br/>'
                dataList = []
              }
            }
          }
        }
      }
      return reStr
    },
    classMapShow (data) {
      let reStr = ''
      let dataList = []
      for (const index in data.class_map) {
        if (index === '0') {
          dataList.push(data.class_map[index])
          if (data.class_map.length <= 1) {
            reStr += this.$t(ipsList[dataList[0].class_type]) + ':' + this.$t(ipsList[dataList[0].class_name]) + ';'
          }
        } else {
          if (data.class_map[index].class_type === data.class_map[index - 1].class_type) {
            dataList.push(data.class_map[index])
          } else {
            for (const childIndex in dataList) {
              if (childIndex === '0') {
                reStr += this.$t(ipsList[dataList[childIndex].class_type]) + ':' + this.$t(ipsList[dataList[childIndex].class_name])
              } else {
                reStr += ',' + this.$t(ipsList[dataList[childIndex].class_name])
              }
              if (Number(childIndex) === dataList.length - 1) {
                reStr += ';'
                dataList = []
              }
            }
            dataList.push(data.class_map[index])
          }
          if (data.class_map.length - 1 === Number(index)) {
            for (const childIndex2 in dataList) {
              if (childIndex2 === '0') {
                reStr += this.$t(ipsList[dataList[childIndex2].class_type]) + ':' + this.$t(ipsList[dataList[childIndex2].class_name])
              } else {
                reStr += ',' + this.$t(ipsList[dataList[childIndex2].class_name])
              }
              if (Number(childIndex2) === dataList.length - 1) {
                reStr += ';'
                dataList = []
              }
            }
          }
        }
      }
      return reStr
    }
  }
}
</script>
